package com.xcc.user.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xcc.common.domain.entry.Ms;
import com.xcc.common.domain.entry.User;
import com.xcc.common.domain.service.MsService;
import com.xcc.common.domain.service.UserService;
import com.xcc.common.redis.RedisUtil;
import com.xcc.common.result.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author frosty
 * @description
 * @date 2019-11-20 20:10:24
 */
//@CrossOrigin /*允许跨域请求*/
@RestController
public class UserController {


    @Autowired
    private MsService msServiceImpl;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private UserService userServiceImpl;
    @Autowired
    private ResultBody resultBody;

    @Value("${server.port}")
    private int port;
    @Autowired
    private UserService userService;

    @GetMapping(value = "/getU")
    public ResultBody getU() {
        return ResultBody.success("token+usercontroller");

    }

    @PostMapping("/register")
    public ResultBody register(User user) {
        user.setId(IdWorker.getIdStr());
        boolean save = userService.save(user);
        return save ? ResultBody.success("注册成功") : ResultBody.error("注册失败");
    }

    // get 使用 RequestParam  post RequestBody
    @GetMapping("/list")/*page 页面大小  current 当前页*/
    public ResultBody list(@RequestParam(required = false) Map<String, Object> map) {
        System.out.println(map);
      /*  System.out.println(size);
         map=new HashMap<>();
        map.put("size",size);
        map.put("current",current);*/
        System.out.println(new ResultBody<>().getPort());
        System.out.println(resultBody.getPort());
        return userService.listByPage(map).setPort(resultBody.getPort());
    }

    @GetMapping("/find/{id}")
    public ResultBody find(@PathVariable("id") String id) {
        if (StringUtils.checkValNull(id)) {
            return ResultBody.error("id 不能为空");
        }
        return ResultBody.success(userService.getById(id));
    }


    @PostMapping("/update")
    public ResultBody update(User user) {
        if (StringUtils.checkValNull(user.getId())) {
            return ResultBody.error("id not is null");
        }
        boolean updateById = userService.updateById(user);
        if (updateById) {
            return ResultBody.success();
        } else {
            return ResultBody.error("update  is failed");
        }

    }

    @GetMapping("/delete/{id}")
    public ResultBody delete(@PathVariable("id") String id) {
        if (StringUtils.checkValNull(id)) {
            return ResultBody.error("id not is null");
        }
        boolean b = userService.removeById(id);
        if (b) {
            return ResultBody.success();
        } else {
            return ResultBody.error("delete is failed");
        }
    }

    @RequestMapping("/login")
    public ResultBody login(String username, String password) {
         return userService.login("admin", "123456");

        // return userService.login(username, password);

    }

    @GetMapping("/ms")
     public  void  add(){

         int id=1;
  for (int i=1;i<1000;i++){
      List list=new ArrayList();
      for (int j=1;j<1000;j++){
          Ms ms=new Ms();
     ms.setSecond(i);
      ms.setMs(j);
      ms.setId(id);
   id++;
        list.add(ms);

      }


      msServiceImpl.saveBatch(list);
  }


     }



}
